

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.common.entity.SampalingEntity;
import com.yz.dao.RuinDao;
import com.yz.dao.SampalingDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sampalingService")
public class SampalingServiceImpl extends ServiceImpl<SampalingDao,SampalingEntity> implements SampalingService {
    @Autowired
    CheckService checkService;
    @Autowired
    DrawingService drawingService;
    @Autowired
    CameraService cameraService;
    @Autowired
    VedioService vedioService;
    @Autowired
    ProjectService projectService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postMe(SampalingForm data,UserEntity user) {
        SampalingEntity t = data.getSampalingEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(t.getUpdateAt());
        check.setCreateUser(t.getCreateUser());
        check.setName(projectService.selectById(t.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[5]);
        check.setProjectId(t.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(102);
        long id = 0;
        if(t.getId()==0){
            this.baseMapper.insert(t);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[5]+id);
            checkService.insert(check);
        }else{
            id = t.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[5]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[5]+id));
            this.baseMapper.update(t,new EntityWrapper<SampalingEntity>().eq("id",id));
        }
        //  type:  A日记，B绘图，C拍照，D视频，E表单，F记录
//  type_form:如果是日记或者图形资料或者记录时：1勘探，2挖掘
//    如果是表单：1区域系统调查记录表,2遗址调查记录，
//  3断面观察记录，4钻探记录,101发掘记录表，102采样记录表,103全站测绘设置表，104全站测绘记录，表105出土器物标本登记表，
//  106陶器登记册，107包含物保存状况表,108墓葬发掘登记表
//        public static String[] type_all_form={"A","B","C","D","E","F"};
//        public static int[] type_detail_form={1,2,3,4,101,102,103,104,105,106,107,108,0};
//        public static String[] type_name_form={"区域系统调查记录表","遗址调查记录表","断面观察记录表","钻探记录表","发掘记录表","采样记录表","全站测绘设置表","全站测绘记录表","出土器物标本登记表","陶器登记册表","包含物保存状况表","墓葬发掘登记表"};
//        public static String[] type_name={"勘探日记","挖掘日记","勘探手绘图","挖掘手绘图","勘探照片","挖掘照片","勘探视频","挖掘视频","勘探记录","挖掘记录"};
//        public static String[] record_name={"探访记录","灰坑记录","房屋记录","陶窑记录","水井记录","壕沟记录","城址记录","其他记录"};
        //图片放入数据库
           cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",102));
        if(data.getDrawingNumber()!=null&& data.getDrawingNumber().size()>0){
            drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",102));
            for (int i = 0; i <data.getDrawingNumber().size() ; i++) {
                DrawingEntity item1 = data.getDrawingNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(t.getCreateAt());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(t.getProjectId());
                item1.setTypeWhere(2);
                item1.setTypeForm(102);
                item1.setDataId(id);
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[5]+id);
                drawingService.insert(item1);
            }
        }
        if(data.getCameraNumber()!=null&& data.getCameraNumber().size()>0){
            for (int i = 0; i <data.getCameraNumber().size() ; i++) {
                 CameraEntity item = data.getCameraNumber().get(i);
            item.setPosterId(user.getUserId());
            item.setPostTime(t.getCreateAt());
            item.setCreateUser(user.getUsername());
            item.setProjectId(t.getProjectId());
            item.setTypeWhere(2);
            item.setTypeForm(102);
            item.setDataId(id);
            item.setFiledName("0");
            item.setNumber(contant.type_all_form[4]+contant.type_detail_form[5]+id);
            cameraService.insert(item);
            }
        }
        if(data.getSchematic()!=null&& data.getSchematic().size()>0){//示意图
            for (int i = 0; i <data.getSchematic().size() ; i++) {
            CameraEntity item = data.getSchematic().get(i);
            item.setPosterId(user.getUserId());
            item.setPostTime(t.getCreateAt());
            item.setCreateUser(user.getUsername());
            item.setProjectId(t.getProjectId());
            item.setTypeWhere(2);
            item.setTypeForm(102);
            item.setDataId(id);
            item.setFiledName("4");
            item.setNumber(contant.type_all_form[4]+contant.type_detail_form[5]+id);
            cameraService.insert(item);
            }
        }
        if(data.getVideosNumber()!=null&& data.getVideosNumber().size()>0){
            for (int i = 0; i <data.getVideosNumber().size() ; i++) {
            vedioService.delete(new EntityWrapper<VedioEntity>().eq("data_id",id).eq("type_form",102));
            VedioEntity item3 = data.getVideosNumber().get(i);
            item3.setPosterId(user.getUserId());
            item3.setPostTime(t.getCreateAt());
            item3.setCreateUser(user.getUsername());
            item3.setProjectId(t.getProjectId());
            item3.setTypeWhere(2);
            item3.setTypeForm(102);
            item3.setDataId(id);
            item3.setPlateNumber(contant.type_all_form[4]+contant.type_detail_form[5]+id);
            vedioService.insert(item3);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteMe(long id) {
        vedioService.delete(new EntityWrapper<VedioEntity>().eq("data_id",id).eq("type_form",102));
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",102));
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",102));
        this.baseMapper.deleteById(id);
        checkService.deleteMe(5,102,id);
    }

    @Override
    public SampalingForm getDetail(long id) {
        SampalingForm data = new SampalingForm();
        SampalingEntity t = this.baseMapper.selectById(id);
        List<DrawingEntity> dds = drawingService.selectList(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",102));
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","0").eq("data_id",id).eq("type_form",102));
        List<CameraEntity> cc2s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","4").eq("data_id",id).eq("type_form",102));
        List<VedioEntity> vvs = vedioService.selectList(new EntityWrapper<VedioEntity>().eq("data_id",id).eq("type_form",102));
        data.setSampalingEntity(t);
        data.setDrawingNumber(dds);
        data.setCameraNumber(cc1s);
        data.setSchematic(cc2s);
        data.setVideosNumber(vvs);
        return data;
    }
}
